﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Orchard.DisplayManagement.Descriptors;
using MediaGarden.Playlists.Services;
using MediaGarden.Models;
using MediaGarden;
using System.Web.Routing;

namespace MediaGarden.Playlists.Shapes
{
    public class PlaylistShapes : IShapeTableProvider
    {
        private readonly Lazy<IPlaylistService> _playlistService;

        public PlaylistShapes(Lazy<IPlaylistService> playlistService)
        {
            _playlistService = playlistService;
        }

        public void Discover(ShapeTableBuilder builder)
        {
            builder.Describe("Media").OnDisplaying(displaying =>
                {
                    var url = Null.Url;
                    IMediaItem item = displaying.Shape.MediaItem;
                    string playlistFormat = "Default";
                    if (displaying.Shape.PlaylistFormat != null)
                    {
                        playlistFormat = displaying.Shape.PlaylistFormat;
                    }
                    if (playlistFormat == "Default")
                    {
                        // Attempt to get default playlist format for current player
                        playlistFormat = _playlistService.Value.DefaultPlaylistFormat(item);
                    }
                    if (item.MediaStereotype == "Playlist")
                    {
                        // TODO: Avoid this processing if the playlist will only be coming from a service instead
                        // Maybe we can just have an interface to hook into here where the playlist format
                        // will decide how to pass it down to the client
                        // TODO: Need a way to request different formats of playlist from the same item, and other options. Maybe an Event hook somewhere in playlistservice.
                        var playlistContext = _playlistService.Value.BuildPlaylist(item, new RouteValueDictionary());
                        // Pass in the IPlaylist so rendering decisions could be made in the view
                        displaying.Shape.Playlist = playlistContext.Playlist;

                        // TODO: Again these URLs could come from the view. Might need a way for performance purposes to avoid generating the playlist (above)
                        // if we're only passing on the URL anyway.

                        if (item is IMediaSource) {
                            displaying.Shape.PlaylistUrl =
                                url.Absolute(url.Action("Preview", "Playlist", new { id = (item as IMediaSource).Record.Id, area = "MediaGarden.Playlists", playlistFormat = playlistFormat }));
                        }
                        if (item is MediaPart)
                        {
                            displaying.Shape.PlaylistUrl =
                                url.Absolute(url.Action("Index", "Playlist", new { id = (item as MediaPart).Id, area = "MediaGarden.Playlists", playlistFormat = playlistFormat }));
                        }
                    }
                    else
                    {
                        // Generate single item playlist URL
                        // TODO: More useful as an extension method directly from the view
                        if (item is IMediaSource)
                        {
                            displaying.Shape.PlaylistUrl = url.Absolute(url.Action("PreviewItem", "Playlist", new { id = (item as IMediaSource).Record.Id, area = "MediaGarden.Playlists", playlistFormat = playlistFormat }));
                        }
                        if (item is MediaPart)
                        {

                            displaying.Shape.PlaylistUrl = url.Absolute(url.Action("Item", "Playlist", new { id = (item as MediaPart).Id, area = "MediaGarden.Playlists", playlistFormat = playlistFormat }));
                        }
                    }

                });
        }
    }
}